var getDayCountOfMonth = function (year, month) {
    if (month === 3 || month === 5 || month === 8 || month === 10) {
        return 30;
    }

    if (month === 1) {
        if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
            return 29;
        } else {
            return 28;
        }
    }

    return 31;
};

var getFirstDayOfMonth = function (date) {
    var temp = new Date(date.getTime());
    temp.setDate(1);
    return temp.getDay();
};

var getWeekNumber = function (src) {
    var date = new Date(src.getTime());
    date.setHours(0, 0, 0, 0);
    // Thursday in current week decides the year.
    date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
    // January 4 is always in week 1.
    var week1 = new Date(date.getFullYear(), 0, 4);
    // Adjust to Thursday in week 1 and count number of weeks from date to week1.
    return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
};




//  获取上一个月,Date对象
function getPreMonth(date) {
    let count = getDayCountOfMonth(date.getFullYear(), date.getMonth());
    if (date.getDate() < 5) {
        count = count - 5;
    }
    return new Date(date.getTime() - 24000 * 3600 * count);
}

//  获取下一个月
function getNextMonth(date) {
    let count = getDayCountOfMonth(date.getFullYear(), date.getMonth());
    if (date.getDate() > count - 5) {
        count = count - 5;
    }

    return new Date(date.getTime() + 24000 * 3600 * count);
}

//测试        
//alert(getPreMonth("2014-01-25"));
//alert(getNextMonth("2014-12-25"));



console.log(getFirstDayOfMonth(new Date()))
console.log(getWeekNumber(new Date()))

console.log(new Date("2018-03-31"));
console.log(getPreMonth(new Date("2018-03-31 23:59:59")))
console.log(getPreMonth(new Date("2018-03-1 23:59:59")))
console.log(getNextMonth(new Date("2018-03-31")))


var a = /^[1-2]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])$/g;
// var a = /^([1-2]\d{3})(.+)$/;

console.log("2018-01-01".match(a));
if (a.test("2018-01-01")) {
    console.log('0k');
} else {
    console.log('fails');
}

/^[1-2]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])$/g.test('2018-9-9');

for (n in [0,1]) {
    console.log(n);
}